package com.jay.android;

import com.jay.android.obd.OBDDevices;
import com.jay.android.view.LCDTextView;
import com.jay.android.view.SpeedView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class DashBoardActivity extends Activity {
	private float mTargetSpeed;
	protected final Handler mSpeedHandler = new Handler();
	protected final Handler mHandler = new Handler();
	private boolean mStopDrawing;
	private SpeedView mSpeedPointer;
	private LCDTextView speedTextView;

	protected Runnable mSpeedViewUpdater = new Runnable() {
		@Override
		public void run() {
			if (mSpeedPointer != null && !mStopDrawing) {
				float mSpeed = mSpeedPointer.getSpeed();
				if (mSpeed != mTargetSpeed) {
					if (mSpeed > mTargetSpeed) {
						mSpeed -= 2;
					} else {
						mSpeed += 2;
					}
					speedTextView.setText(String.format("%03d", (int) mSpeed));
					mSpeedPointer.updateSpeed(mSpeed);
					mSpeedHandler.postDelayed(mSpeedViewUpdater, 20);
				}

			}
		}
	};

	protected Runnable mRunnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			if (!mStopDrawing) {
				OBDDevices.getInstance().getSpeed();
				mHandler.postDelayed(mRunnable, 500);
			}
		}
	};

	private final Handler mOBDHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case OBDDevices.OBD_CONNECT_STATE:
				int state = msg.arg1;
				if (state == OBDDevices.OBD_CONNECT_STATE_CONNECTED) {
					// statusTextView.setText("已经成功连接上OBD盒子");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_DISCONNECTED) {
					// statusTextView.setText("OBD盒子失去连接");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_CONNECTING) {
					// statusTextView.setText("正在连接中...");
				} else if (state == OBDDevices.OBD_CONNECT_STATE_TIMEOUT) {
					// statusTextView.setText("连接超时，请尝试重新连接");
				}
				break;

			case OBDDevices.OBD_MESSAGE_COMMING:
				switch (msg.arg1) {
				case OBDDevices.OBD_MESSAGE_SPEED:
					int speed = msg.arg2;
					mTargetSpeed = speed;
					mTargetSpeed = mTargetSpeed / 2 * 2;
					mSpeedHandler.post(mSpeedViewUpdater);
					break;

				default:
					break;
				}
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dash_board);
		speedTextView = (LCDTextView) findViewById(R.id.tv_speed);
		mSpeedPointer = (SpeedView) findViewById(R.id.speed_pointer);
		mTargetSpeed = 0;
		mSpeedHandler.postDelayed(mSpeedViewUpdater, 10);
		//OBDDevices.getInstance().init(this);

		mHandler.postDelayed(mRunnable, 2000);

		Button testButton = (Button) findViewById(R.id.test);
		testButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				// TODO Auto-generated method stub
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		mStopDrawing = false;
		OBDDevices.getInstance().register(mOBDHandler);
	}

	@Override
	protected void onPause() {
		super.onPause();
		mStopDrawing = true;
		OBDDevices.getInstance().unregister();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//OBDDevices.getInstance().release();
	}
}
